﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CF.Data.Entity.FrmMemberEntity;

namespace CF.Member.DAL.Badges
{
    public abstract class BaseBadge : IBadge
    {
        protected readonly int BaseAwardExp;
        protected readonly int BaseAwardCents;
        protected readonly byte BaseBadgeLevel;
        protected readonly byte MaxBadgeLevel;

        public BaseBadge(int defAwdExp, int defAwdCents, byte defBadgeLvl,byte defMaxLvl)
        {
            BaseAwardExp = defAwdExp;
            BaseAwardCents = defAwdCents;
            BaseBadgeLevel = defBadgeLvl;
            MaxBadgeLevel = defMaxLvl;
        }

        public BaseBadge()
        {
            BaseAwardExp = 0;
            BaseAwardCents = 0;
            BaseBadgeLevel = 0;
        }

        public virtual bool CheckTakeCondition(int uid, int bid)
        {
            bool result = false;
            using (caifendbEntities context = new caifendbEntities())
            {
                FM_MyBadges mybd = context.FM_MyBadges.FirstOrDefault(c => c.BID == bid && c.UID == uid);
                if (mybd == null)
                {
                    result = true;
                }
            }
            return result;
        }

        public virtual UpdateBadgeStatus UpdateBadgeLevel(int bid, int uid)
        {
            throw new NotImplementedException("徽章无法升级");
        }

        public virtual bool TakeBadge(int bid, int uid)
        {
            bool result = false;
            using (caifendbEntities context = new caifendbEntities())
            {
                FM_BadgeConfig bdcfg = context.FM_BadgeConfig.FirstOrDefault(c => c.BID == bid);
                if (bdcfg != null)
                {
                    FM_MyBadges myBadge = new FM_MyBadges();
                    myBadge.UID = uid;
                    myBadge.BID = bid;
                    myBadge.BadgeLevel = BaseBadgeLevel;
                    myBadge.CurrAwardCents = bdcfg.AwardCents ?? BaseAwardCents;
                    myBadge.CurrAwardExp = bdcfg.AwardExperience ?? BaseAwardExp;
                    myBadge.CurrParam = 0;
                    myBadge.GetBadgeTime = DateTime.Now;
                    myBadge.IsShow = BadgeShowState.Show;
                    myBadge.Status = BadgeActState.Unactuated;
                    context.AddToFM_MyBadges(myBadge);
                    context.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public virtual bool ActivationBadge(int bid, int uid)
        {
            bool result = false;
            using (caifendbEntities context = new caifendbEntities())
            {
                FM_MyBadges myBadge = context.FM_MyBadges.FirstOrDefault(c => c.UID == uid && c.BID == bid && c.Status == BadgeActState.Unactuated);
                if (myBadge != null)
                {
                    myBadge.LastUpdateTime = DateTime.Now;
                    myBadge.Status = BadgeActState.Activation;
                    context.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }

    public enum UpdateBadgeStatus
    {
        Default,
        NotFound,
        UpdateSuccess,
        UpdateFailed,
        AddGrowUpPoint,
        FullLevel,
        Error
    }
}
